import Reqwest from '../../utils/Reqwest';
import {AdBean} from './AdBean';
import {base, assign, redirect} from '../base/Base';
import {ResponseDto} from '../base/BaseDTO';
import {TagBean} from "../tag/TagBean";

declare let GLOBAL: any;

export class AdService {

    path = `${GLOBAL.baseApiUrl}/ad/api`;

    searchAdList(params: any, options = {}): Promise<any> {
        return new Reqwest(assign({
            url: `${this.path}/list.jhtml`,
            method: 'get',
            type: 'json',
            contentType: 'application/json; charset=UTF-8',
            data: params || {},
            headers: {
                'X-CSRF-TOKEN': base._csrf
            }
        }, options)).catch(redirect);
    }

    adDelete(params: {
        id: number
    }, options = {}): Promise<ResponseDto<any>> {
        return new Reqwest(assign({
            url: `${this.path}/delete.jhtml`,
            method: 'get',
            type: 'json',
            contentType: 'application/json; charset=UTF-8',
            data: params || {},
            headers: {
                'X-CSRF-TOKEN': base._csrf
            }
        }, options)).catch(redirect);
    }

    loadEditByAdId(params: {
        id: number
    }, options = {}): Promise<ResponseDto<AdBean<any>>> {
        return new Reqwest(assign({
            url: `${this.path}/getCreateDtoById.jhtml`,
            method: 'get',
            type: 'json',
            contentType: 'application/json; charset=UTF-8',
            data: params || {},
            headers: {
                'X-CSRF-TOKEN': base._csrf
            }
        }, options)).catch(redirect);
    }

    adEdit(params: AdBean<any>, options = {}): Promise<ResponseDto<any>> {
        return new Reqwest(assign({
            url: `${this.path}/edit.jhtml`,
            method: 'post',
            type: 'json',
            contentType: 'application/json; charset=UTF-8',
            data: JSON.stringify(params) || {},
            headers: {
                'X-CSRF-TOKEN': base._csrf
            }
        }, options)).catch(redirect);
    }

    /**
     * 新增
     */
    adAdd(params: AdBean<any>, options = {}): Promise<ResponseDto<any>> {
        return new Reqwest(assign({
            url: `${this.path}/add.jhtml`,
            method: 'POST',
            type: 'json',
            contentType: 'application/json; charset=UTF-8',
            data: JSON.stringify(params) || {},
            headers: {
                'X-CSRF-TOKEN': base._csrf
            }
        }, options)).catch(redirect);
    }

    /**
     * 获取API
     */
    getAdapiUrlById(params: {
        id: number
    }, options = {}): Promise<any> {
        return new Reqwest(assign({
            url: `${this.path}/getAdUrlById.jhtml`,
            method: 'get',
            type: 'json',
            contentType: 'application/json; charset=UTF-8',
            data: params || {},
            headers: {
                'X-CSRF-TOKEN': base._csrf
            }
        }, options)).catch(redirect);
    }

    getPositionByMediaType(params: {
        type: number
    }, options = {}): Promise<ResponseDto<any>> {
        return new Reqwest(assign({
            url: `${GLOBAL.baseApiUrl}/adConfigPosition/api/types.jhtml`,
            method: 'get',
            type: 'json',
            contentType: 'application/json; charset=UTF-8',
            data: params || {},
            headers: {
                'X-CSRF-TOKEN': base._csrf
            }
        }, options)).catch(redirect);
    }

    bindPositionByMediaType(params: {
        id: number,
        adPositionIds: Array<string>
    }, options = {}): Promise<ResponseDto<any>> {
        return new Reqwest(assign({
            url: `${this.path}/updateBindPositionById.jhtml`,
            method: 'post',
            type: 'json',
            contentType: 'application/json; charset=UTF-8',
            data: JSON.stringify(params) || {},
            headers: {
                'X-CSRF-TOKEN': base._csrf
            }
        }, options)).catch(redirect);
    }

    getPositionInfosById(params: {
        id: number
    }, options = {}): Promise<ResponseDto<any>> {
        return new Reqwest(assign({
            url: `${this.path}/getPositionInfosById.jhtml`,
            method: 'get',
            type: 'json',
            contentType: 'application/json; charset=UTF-8',
            data: params || {},
            headers: {
                'X-CSRF-TOKEN': base._csrf
            }
        }, options)).catch(redirect);
    }

    findTags(params = {}, options = {}): Promise<ResponseDto<TagBean<any>>> {
        return new Reqwest(assign({
            url: `${this.path}/findTags.jhtml`,
            method: 'get',
            type: 'json',
            contentType: 'application/json; charset=UTF-8',
            data: params || {},
            headers: {
                'X-CSRF-TOKEN': base._csrf
            }
        }, options)).catch(redirect);
    }
}